require('../../assets/css/reset.css');
require('../../assets/css/property.css');
require('../password/password.less');
// 引入插件
const CaptchaMini = require('captcha-mini')

//导入请求实例
const request = require('../../assets/utils/request')
console.log('request :>> ', request);

window.onload = () => {

    //获取dom
    const phoneIpt = document.querySelector('#phoneIpt')
    const codeIpt = document.querySelector('#codeIpt')
    const pwdIpt = document.querySelector('#pwdIpt')
    const pwdTwiceIpt = document.querySelector('#pwdTwiceIpt')
    const submit = document.querySelector('#submit')



    /*不传值，统一走默认值*/
    let captcha1 = new CaptchaMini({
        fontSize: 30,           //字体大小
        dotNum: 100,       //点的数量
        length: 4    //验证码长度
    });
    let captcha = ''
    captcha1.draw(document.querySelector('#captcha1'), r => {
        captcha = r
        console.log(r, '验证码1');
    });

    //点击注册按钮
    submit.addEventListener('click', () => {
        //收集错误信息
        let errMsg = ''
        //开始验证
        if (!/^1[3-9]\d{9}$/.test(phoneIpt.value)) {
            errMsg = '手机号格式不正确'
            //验证码全部转为小写 方便用户
        } else if (codeIpt.value.toLowerCase() !== captcha.toLowerCase()) {
            errMsg = '验证码错误'
        } else if (!/^\w{3,6}$/.test(pwdIpt.value)) {
            errMsg = '密码3-6位'
        } else if (pwdIpt.value !== pwdTwiceIpt.value) {
            errMsg = '密码不一致'
        } else {
            console.log('验证通过 可以发送请求')

            //发送请求检测 账号是否存在
            request
                .post('/api/user/isExist', {
                    account: phoneIpt.value,
                })
                .then(res => {
                    console.log('res :>> ', res)
                    //解构数据
                    let { errno, message } = res.data
                    //判断
                    if (errno === 0) {
                        //发送请求
                    } else {
                        alert(message)
                    }
                })
                .catch(err => {
                    console.log('err :>> ', err)
                })
        }

        if (errMsg) {
            alert(errMsg)
        }
    })
}
